﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using AIC_PN.Reports;
namespace AIC_PN
{
    public partial class MyReportViewMau3 : MyFromBase
    {

        protected void Page_Init(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = new SqlConnection(SqlHelper.connectionString);
                SqlCommand cmd = new SqlCommand(MyReportSQL.Replace("#REPLACE#", " B.BoiDen = 1 "), conn);

                for (int i = 0; i < MyReportParams.Count; i++)
                {
                    cmd.Parameters.AddWithValue(MyReportParams[i].ParameterName, MyReportParams[i].Value);
                }
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                grvData.DataSource = dt;
                grvData.DataBind();

                cmd = new SqlCommand(MyReportSQL.Replace("#REPLACE#", " B.BoiDen = 2 "), conn);

                for (int i = 0; i < MyReportParams.Count; i++)
                {
                    cmd.Parameters.AddWithValue(MyReportParams[i].ParameterName, MyReportParams[i].Value);
                }
                SqlDataAdapter da2 = new SqlDataAdapter(cmd);
                DataTable dt2 = new DataTable();
                da2.Fill(dt2);
                grvData2.DataSource = dt2;
                grvData2.DataBind();

                #region Add param


                if (!MyReportSQLParam.Equals(""))
                {
                    List<SqlParameter> lstParams = new List<SqlParameter>();

                    DataTable dtParam = SqlHelper.ExecuteDataTable(CommandType.Text, MyReportSQLParam, MyReportParams);
                    if (dtParam.Rows.Count > 0)
                    {
                        lblTenBaoCao.Text = dtParam.Rows[0]["CM_TenBaoCao"].ToString();
                        lblDonViBaoCao.Text = dtParam.Rows[0]["TenDV"].ToString();
                        lblNgayBaoCao.Text = dtParam.Rows[0]["CM_ThoiGianBaoCao"].ToString();
                    }
                }
                #endregion Add param

                //printReport.ReportSource = objReport;
                //printReport.DataBind();


            }
            catch (Exception ex)
            {
                Response.Write("Exception Message: " + ex.StackTrace + "<br>");
            }
        }

        protected void grvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.Header)
            //{
            //    e.Row.Cells[0].ColumnSpan = 2;
            //    e.Row.Cells[1].Visible = false;
            //}
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView dtr = (DataRowView)e.Row.DataItem;
                if (e.Row.Cells[0].Text.Equals(e.Row.Cells[1].Text))
                {
                    e.Row.Cells[0].ColumnSpan = 2;
                    e.Row.Cells[0].Text = (dtr.Row.ItemArray[4].ToString().Equals("0") ? "" : " ") + e.Row.Cells[0].Text;
                    e.Row.Cells[0].Font.Bold = !dtr.Row.ItemArray[4].ToString().Equals("0") ? true : false;
                    e.Row.Cells[1].Visible = false;
                }
            }
            RowDataBound(sender, e);
        }

        protected void grvData_PreRender(object sender, EventArgs e)
        {
        }

        protected void grvData2_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {

                GridView objGridView = (GridView)sender;
                //GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                GridViewRow objgridviewrow = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);
                GridViewRow objgridviewrow2 = new GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert);

                TableCell objtablecell = new TableCell();
                AddMergedCells(objgridviewrow, objtablecell, 2, 2, "NỘI DUNG");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "ĐƠN VỊ TÍNH");
                AddMergedCells(objgridviewrow, objtablecell, 1, 2, "TỔNG SỐ");
                AddMergedCells(objgridviewrow, objtablecell, 3, 1, "Chia ra");
                objgridviewrow.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(0, objgridviewrow);

                objtablecell = new TableCell();
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Công lập");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Dân lập");
                AddMergedCells(objgridviewrow2, objtablecell, 1, 1, "Tư thục");
                objgridviewrow2.Font.Bold = true;
                objGridView.Controls[0].Controls.AddAt(1, objgridviewrow2);
            }
        }

        protected void grvData2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].ColumnSpan = 2;
                e.Row.Cells[1].Visible = false;
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView dtr = (DataRowView)e.Row.DataItem;
                if (e.Row.Cells[0].Text.Equals(e.Row.Cells[1].Text))
                {
                    e.Row.Cells[0].ColumnSpan = 2;
                    e.Row.Cells[0].Text = (dtr.Row.ItemArray[4].ToString().Equals("0") ? "" : " ") + e.Row.Cells[0].Text;
                    e.Row.Cells[0].Font.Bold = !dtr.Row.ItemArray[4].ToString().Equals("0") ? true : false;
                    e.Row.Cells[1].Visible = false;
                }
            }
            RowDataBound(sender, e);
        }

        protected void grvData2_PreRender(object sender, EventArgs e)
        {
            MergeRows(grvData2);
        }

        private static void AddMergedCells(GridViewRow objgridviewrow, TableCell objtablecell, int colspan, int rowspan, string celltext)
        {
            objtablecell = new TableCell();
            objtablecell.Text = celltext;
            objtablecell.ColumnSpan = colspan;
            objtablecell.RowSpan = rowspan;
            objtablecell.HorizontalAlign = HorizontalAlign.Center;
            objgridviewrow.Cells.Add(objtablecell);
        }

        public void MergeRows(GridView gridView)
        {
            for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
            {
                GridViewRow row = gridView.Rows[rowIndex];
                GridViewRow previousRow = gridView.Rows[rowIndex + 1];

                if (row.Cells[0].Text == previousRow.Cells[0].Text)
                {
                    row.Cells[0].RowSpan = previousRow.Cells[0].RowSpan < 2 ? 2 : previousRow.Cells[0].RowSpan + 1;
                    previousRow.Cells[0].Visible = false;
                }

                if (row.Cells[0].Text.Trim() == previousRow.Cells[0].Text.Trim() && row.Cells[0].Text.Trim() == row.Cells[1].Text.Trim())
                {
                    row.Visible = false;
                }
            }
        }

    }
}
